﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Text;
using System.Collections;

namespace FOBVT.Web.DBUtility
{
	/// <summary>
	/// 向数据库插入记录的方法类
	/// </summary>
	public class InsertMethod : BaseMethod
	{
		private static Hashtable InsertClauseStore = new Hashtable();
		public InsertMethod(Type infoType)
			: base(infoType)
		{
		}
		public InsertMethod(BaseInfo info)
			: base(info)
		{
		}
		public InsertMethod(string TableName, BaseInfo info)
			: base(TableName, info)
		{
		}

		public bool Insert()
		{
			string CommandText = InsertClause;
			bool result = base.ExecuteBool(CommandText, base.GetParameters(false));
			return result;
		}

		protected string InsertCommandText(StringBuilder sb_insert)
		{
			sb_insert.AppendFormat("INSERT INTO [{0}] (", base.TableName);
			base.AppendParameterString(sb_insert, "[{0}]", ", ", false);
			sb_insert.Append(") VALUES (");
			base.AppendParameterString(sb_insert, "@{0}", ", ", false);
			sb_insert.Append(");");
			return sb_insert.ToString();
		}

		/// <summary>
		/// 插入SQL
		/// </summary>
		public string InsertClause
		{
			get
			{
				string result = InsertClauseStore[base.TableName] as string;
				if (result == null)
				{
					StringBuilder sb_insert = new StringBuilder();
					this.InsertCommandText(sb_insert);
					InsertClauseStore[base.TableName] = result = sb_insert.ToString();
				}
				return result;
			}
		}
	}
}